/**
 * 表格搜索插件
 * 依赖于zTree插件
 * 参数：
 * @element 绑定导航的div对象
 * @options Object 参数对象
 * @options.oMetadata 元数据模块对象
 * @options.tableColumnData 表头数据
 * */

define(function(require, exports,module){
    require("commonStaticDirectory/plugins/quickMenu/quickMenu.css");

    require("commonStaticDirectory/plugins/zTree/zTreeStyle/zTreeStyle.css");
    require("commonStaticDirectory/plugins/zTree/jquery.ztree.all.min.js");
    require("commonStaticDirectory/plugins/artDialog/ui-dialog.css"); //artDialog弹窗样式
    require("commonStaticDirectory/plugins/jquery.dragsort"); //拖动排序组件
    var tools = require("commonStaticDirectory/plugins/tools");
    var Ajax = require("commonStaticDirectory/plugins/ajax.js");

    var quickMenu = function(){
        this.init.apply(this,arguments);
    };
    quickMenu.prototype = {
        /** 构造函数 */
        constructor: quickMenu,
        opts:{},
        init:function(element, options){
            this.$el = $(element);
            this.opts = $.extend({},this.opts,options);
            this.infoSetId = this.opts.oMetadata.options.infoSetId;
            this._initLayout();
        },
        //初始化dom结构
        _initLayout: function () {
            var t = this;
            var $el = this.$el;
            var opts = this.opts;
            t.tableColumnData = opts.tableColumnData;
            var aNavigationData = opts.tableColumnData.navigationData;
            t.aNavigationData = aNavigationData;

            $el.addClass("mmPositionTools");
            var pgHtmls = '<div class="l treeOrgNamesWrap">' +
                '<div class="position_icon iconfont_dayhr_base"><span>&#xe609;</span> <div id="treeOrgNames" class="ztree myztree"></div></div>' +
                '       <div class="position_organi_a l">'+
                '            <div class="l position_children_orgName">'+
                '            </div>'+
                '        </div>'+
                '</div>';
            t.$html = $(pgHtmls);
            $el.html(t.$html);

            var aZTreeData = [];

            //初始化树
            t.orgTreeSetting = {
                view:{showIcon:false},
                callback:{
                    //树的点击
                    onClick:function(event, treeId, treeNode){
                        var newTreeNode = $.extend(true,{},treeNode); //弄一份新的防止被污染

                        $el.find(".position_children_orgName").html(t.setNavShow(newTreeNode));

                        t.selectedNavNode = newTreeNode; //当前的导航节点
			            t.opts.oMetadata.options.navigationId = newTreeNode.id;
                        //填充导航并初始化表格数据
                        //如果表格需要刷新
                        if(newTreeNode.isRefreshTableStyle){
                            t.tableColumnData.isRefreshTableStyle = true;
                            t.tableColumnData.navigationCondition = newTreeNode.condition;
                            t.opts.oMetadata.initTabel(t.tableColumnData,{navigationCondition:newTreeNode.condition}); //重载表格数据
                        }else{  //如果不需要刷新
                            t.tableColumnData.isRefreshTableStyle = false;
                            t.opts.oMetadata.queryData.navigationCondition = newTreeNode.condition;
                            t.opts.oMetadata.mmg.load();
                        }
                        t.selectedNodeCondition = newTreeNode.condition; //把当前导航的条件缓存起来


                        $("#treeOrgNames").hide();
                    }
                }
            };
            if(t.aNavigationData && t.aNavigationData.length) {
                aZTreeData = t.aNavigationData;
            }else{
                $("#mmPositionTools").find(">div.l").hide();
            }
            $.fn.zTree.init($("#treeOrgNames"),t.orgTreeSetting , aZTreeData);
            t.treeNavObj = $.fn.zTree.getZTreeObj("treeOrgNames");

            //递归选中不是通过点击的当前节点
            var nodes = t.treeNavObj.getNodes();
            //如果有导航数据的时候
            if (nodes.length>0) {
                var isMatchingNavId = false;
                var oSelectedNode = null;

                function setSelectedNode(aNodes){
                    if(aNodes.length){
                        for(var i=0;i<aNodes.length;i++){
                            if(aNodes[i].id == t.opts.oMetadata.options.navigationId){
                                isMatchingNavId = true;
                                oSelectedNode = aNodes[i]; //当前的导航节点
                                break;
                            }
                            if(aNodes[i].children && aNodes[i].children.length){
                                setSelectedNode(aNodes[i].children);
                            }
                        }
                    }
                }
                setSelectedNode(nodes);

                if(!isMatchingNavId){
                    oSelectedNode = nodes[0];
                }

                //填充导航并初始化表格数据
                t.selectedNavNode = oSelectedNode; //当前的导航节点
                t.opts.oMetadata.options.navigationId = oSelectedNode.id;
                t.treeNavObj.selectNode(oSelectedNode);
                $el.find(".position_children_orgName").html(t.setNavShow(oSelectedNode));
                t.opts.oMetadata.initTabel(t.tableColumnData,{navigationCondition:oSelectedNode.condition}); //重载表格数据
                t.selectedNodeCondition = oSelectedNode.condition; //把当前导航的条件缓存起来


            }else{
                //如果没有导航的时候，就只是加载表格
                t.tableColumnData.isRefreshTableStyle = false;
                t.opts.oMetadata.initTabel(t.tableColumnData); //重载表格数据
            }

            t.bindEvent();
        },
        bindEvent:function(){
            var t = this;
            var $el = this.$el;

            //导航树的弹出的隐藏
            $el.find(".treeOrgNamesWrap").hover(function(){
                $("#treeOrgNames").slideDown(200);
            },function(){
                $("#treeOrgNames").hide();
            });

        },
        //设置导航显示路径
        setNavShow:function(treeNode){
            var arr = [];
            function getNodePath(node){
                if(node){
                    arr.push(node);
                    getNodePath(node.getParentNode());
                }
                return arr;
            }
            var aNodes = getNodePath(treeNode);
            aNodes.length && (aNodes.reverse());
            var sHtml = "";
            if(aNodes.length){
                for(var i= 0,len=aNodes.length;i<len;i++){
                    sHtml +=  '            <div class="l position_orgName">'+
                    '                <span>'+aNodes[i].name+'</span>'+
                    '                <span class="iconfont_dayhr_base">'+(i==len-1?'':'/')+'</span>'+
                    '            </div>';
                }
            }
            return sHtml;
        },

        //重新更新导航树
        updateTreeDataAndDom:function(options){
            options = options || {};
            var t = this;
            var oSend = {infoSetId: t.infoSetId};
            if(options.oParam && !$.isEmptyObject(options.oParam)){
                $.extend(oSend,options.oParam);
            }
            Ajax.ajax({
                url: (gMain.apiBasePath?gMain.apiBasePath:gMain.basePath) +"route/"+ t.infoSetId +"/getTableColumn.do",
                data: JSON.stringify(oSend),
                beforeSend:function () {
                    $("body").loading({zIndex:9999});
                },
                complete:function () {
                    $("body").loading({state:false});
                },
                success: function(data){
                    if(data.result == "true"){
                        if(data.navigationData && data.navigationData.length){
                            var aNodes = t.treeNavObj.getSelectedNodes();
                            $.fn.zTree.init($("#treeOrgNames"),t.orgTreeSetting , data.navigationData);
                            t.treeNavObj = $.fn.zTree.getZTreeObj("treeOrgNames");
                            // if(!aNodes.length){
                            //     aNodes = t.treeNavObj.getNodes();
                            // }
                            aNodes = t.treeNavObj.getNodes();
                            t.treeNavObj.selectNode(aNodes[0]);
                            t.$el.find(".position_children_orgName").html(t.setNavShow(aNodes[0]));
                            typeof options.complate == "function" && options.complate(data.navigationData); //成功后把最新的导航数据回调回去
                            if(options.isReloadMmg){
                                setTimeout(function(){
                                    location.reload(); //刷新页面
                                },2000);
                            }
                        }
                    }
                }
            })
        }
        /**
         * 获取选中的导航
         * */
        ,getSelectedItem: function(){
            return this.selectedItem;
        }

    };

    module.exports = quickMenu;
});
